﻿using EOM.TSHotelManagement.Common.Core;
using EOM.TSHotelManagement.Common.Util;
using Microsoft.AspNetCore.Http;
using SqlSugar;

namespace EOM.TSHotelManagement.EntityFramework
{
    public class GenericRepository<T> : SimpleClient<T> where T : class, new()
    {

        /// <summary>
        /// HTTP上下文访问器
        /// </summary>
        private readonly IHttpContextAccessor _httpContextAccessor;

        private readonly JWTHelper _jWTHelper;

        public GenericRepository(ISqlSugarClient client, IHttpContextAccessor httpContextAccessor, JWTHelper jWTHelper) : base(client)
        {
            base.Context = client;
            _httpContextAccessor = httpContextAccessor;
            _jWTHelper = jWTHelper;
        }

        private string GetCurrentUser()
        {
            try
            {
                var authHeader = _httpContextAccessor?.HttpContext?.Request?.Headers["Authorization"];
                if (string.IsNullOrEmpty(authHeader)) return "System";

                var token = authHeader.ToString().Replace("Bearer ", "", StringComparison.OrdinalIgnoreCase);

                return _jWTHelper.GetSerialNumber(token);
            }
            catch
            {
                return "System";
            }
        }

        public override bool Insert(T entity)
        {
            if (entity is BaseEntity baseEntity)
            {
                var currentUser = GetCurrentUser();
                baseEntity.DataInsDate = DateTime.Now;
                baseEntity.DataInsUsr = currentUser;
            }
            return base.Insert(entity);
        }

        public override bool Update(T entity)
        {
            if (entity is BaseEntity baseEntity)
            {
                var currentUser = GetCurrentUser();
                baseEntity.DataChgDate = DateTime.Now;
                baseEntity.DataChgUsr = currentUser;
            }
            return base.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true, false, true).ExecuteCommand() > 0;
        }

        public override bool UpdateRange(List<T> updateObjs)
        {
            foreach (var entity in updateObjs)
            {
                if (entity is BaseEntity baseEntity)
                {
                    var currentUser = GetCurrentUser();
                    baseEntity.DataChgDate = DateTime.Now;
                    baseEntity.DataChgUsr = currentUser;
                }
            }

            return base.Context.Updateable(updateObjs).IgnoreColumns(ignoreAllNullColumns: true, false, true).ExecuteCommand() > 0;
        }

        public bool SoftDelete(T entity)
        {
            if (entity is BaseEntity baseEntity)
            {
                var currentUser = GetCurrentUser();
                baseEntity.DataChgDate = DateTime.Now;
                baseEntity.DataChgUsr = currentUser;
            }
            return base.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true, false, true).ExecuteCommand() > 0;
        }
    }
}
